home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / OBJECT.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  7KB  |  266 lines

  1. #include "object.h"
  2. #include "oopsconf.h"
  3. #include <stdlib.h>    // for exit
  4.  
  5.  
  6. void Object::printOn(ostream&) const
  7. {
  8.     DTerror("Derived Class Responsibility:", "Object::printOn");
  9.     exit(1);
  10. }
  11.  
  12. bool Object::isEqual(const Object&) const
  13. {
  14.     DTerror("Derived Class Responsibility:", "Object::isEqual");
  15.     return NO;
  16. }
  17.  
  18. unsigned Object::hash() const
  19. {
  20.     derivedClassResponsibility("hash");
  21.     return 0;
  22. }
  23.  
  24. int Object::compare(const Object&) const
  25. {
  26.     DTerror("derivedClassResponsibility", "compare");
  27.     DTerror("Derived Class Responsibility:", "Object::compare");
  28.     return 0;
  29. }
  30.  
  31. Object* Object::copy() const
  32. {
  33.     return deepCopy();
  34. }
  35.  
  36. unsigned Object::size() const { return 0; }       // # of objects in Array.container subclass
  37.  
  38. unsigned Object::capacity() const { return 0; }   // subClass.capacity
  39.  
  40. const Class* Object::isA() const
  41. {
  42.     DTerror("isA() function not defined for some class: ","");
  43.     exit(1);
  44.     return 0;
  45. }
  46.  
  47. Object* Object::shallowCopy() const
  48. {
  49. #if 0
  50. //-gmv debug stuff
  51. // div_sizeof_ptr is machine dependent
  52. // see oopsconf(ig).h
  53.     cout << "HELLO FROM ======Object* Object::shallowCopy()===\\/===\n";
  54.     Class *clss = (Class*)isA();
  55.     unsigned clssSize = clss->size();
  56.     unsigned voidSize = (sizeof(void*));
  57.     unsigned objSize = div_sizeof_ptr(clssSize+(sizeof(void*)-1));
  58. #endif
  59.     unsigned i = div_sizeof_ptr((isA()->size()+(sizeof(void*)-1)));
  60.     //unsigned i = div_sizeof_ptr((((Class*)isA())->size()+(sizeof(void*)-1)));
  61.  
  62.     void** p = (void**)this;
  63.     void** q = new void*[i];
  64.     Object* o = (Object*)q;
  65.     while (i--) *q++ = *p++;
  66.     return o;
  67. }
  68.  
  69. #include "identdic.h"
  70. #include "assoc.h"
  71. static IdentDict* deepCopyDict =0;  // object ID -> object copy
  72.                                     // dictionary for deepCopy
  73.  
  74. Object* Object::deepCopy() const
  75. {
  76. #if 0
  77.     cout << "error: Object::deepCopy() not implemented\n";
  78.     exit(1);
  79.     return (Object*)0;
  80. #else
  81.     bool firstObject = NO;
  82.     if (deepCopyDict == 0) {
  83.         deepCopyDict = new IdentDict;
  84.         deepCopyDict->add(*new Assoc(*nil,*nil));
  85.         firstObject = YES;
  86.     }
  87.     Assoc* asc = (Assoc*)&deepCopyDict->assocAt(*this);
  88.     if (asc == nil) {           // object has not been copied 
  89.         Object* copy = Object::shallowCopy();   // make a shallow copy 
  90.         deepCopyDict->add(*new Assoc(*this,*copy)); // add to dictionary 
  91.         copy->deepenShallowCopy();  // convert shallow copy to deep copy 
  92.         if (firstObject) {      // delete the deepCopy dictionary 
  93.             DO(*deepCopyDict,Assoc*,asc) delete asc; DONE
  94.             delete deepCopyDict;
  95.             deepCopyDict = 0;
  96.         }
  97.         return copy;
  98.     }
  99.     else return asc->value();   // object already copied,
  100.                                 // just return object reference
  101. #endif
  102. }
  103.  
  104. void Object::deepenShallowCopy()
  105. {
  106.     derivedClassResponsibility("deepenShallowCopy");
  107. }
  108.  
  109.  
  110. const Class* Object::species() const  { return isA(); }
  111.  
  112.  
  113. bool Object::isKindOf(const Class& clid) const
  114. {
  115.     const Class* cl = isA();
  116.     do {
  117.         if (cl == &clid) return YES;
  118.         cl = cl->baseClass();
  119.     } while (cl != (Class*) 0);
  120.     return NO;
  121. }
  122.  
  123. void Object::derivedClassResponsibility(const char* fname) const
  124. {
  125.     DTerror("", "");
  126.     cerr << "Unimplemented function: "
  127.          << this << "->"
  128.          << className() << "::"
  129.          << fname
  130.          << "\n";
  131. }
  132.  
  133. void Object::shouldNotImplement(const char* fname) const
  134. {
  135.     DTerror("Illegal function: ",fname);
  136. }
  137.  
  138. void invalidArgClass(const Object& ob,
  139.                      const Class& expect,
  140.                      const char* fname)
  141. {
  142.     DTerror("Invalid argument class.", "");
  143.     cerr << "Expected "
  144.          << fname << "(" << expect.className() << ")"
  145.          << ", was "
  146.          << fname << "(" << ob.className() << ")"
  147.          << "\n";
  148. }
  149.  
  150. void invalidArgSpecies(const Object& ob,
  151.                        const Class& expect,
  152.                        const char* fname)
  153. {
  154.     DTerror("Invalid argument species: ", "");
  155.     cerr << "Expected "
  156.          << fname << "(" << expect.className() << ")"
  157.          << ", was "
  158.          << fname << "(" << ob.species()->className() << ")"
  159.          << "\n";
  160. }
  161.  
  162. void Object::invalidArgClass(const Object& ob,
  163.                              const Class& expect,
  164.                              const char* fname) const
  165. {
  166.     DTerror("Invalid argument class: ", "");
  167.     cerr << "Expected "
  168.          << className() << "::"
  169.          << fname << "(" << expect.className() << ")"
  170.          << ", was "
  171.          << className() << "::"
  172.          << fname << "(" << ob.className() << ")"
  173.          << "\n";
  174. }
  175.  
  176. void Object::invalidArgSpecies(const Object& ob,
  177.                                const Class& expect,
  178.                                const char* fname) const
  179. {
  180.     DTerror("Invalid argument species: ", "");
  181.     cerr << "Expected "
  182.          << className() << "::"
  183.          << fname << "(" << expect.className() << ")"
  184.          << ", was "
  185.          << className() << "::"
  186.          << fname << "(" << ob.species()->className() << ")"
  187.          << "\n";
  188. }
  189.  
  190. void invalidClass(const Object& ob, const Class& expect)
  191. {
  192.     DTerror("Invalid argument class: ", "");
  193.     cerr << "Expected "
  194.          << expect.className()
  195.          << ", was "
  196.          << ob.className()
  197.          << "\n";
  198. }
  199.  
  200. void invalidSpecies(const Object& ob, const Class& expect)
  201. {
  202.     DTerror("Invalid object species: ", "");
  203.     cerr << "Expected "
  204.          << expect.className()
  205.          << ", was "
  206.          << ob.species()->className()
  207.          << "\n";
  208. }
  209.  
  210.  
  211. //====object.cpp====/\
  212.  
  213.  
  214.  
  215. //====class.cpp====\/
  216.  
  217. Class::Class(const Class& super, const char* name, unsigned size)
  218. {
  219.     superClass = &super;
  220.     class_name = name;
  221.     inst_size = size;
  222. }
  223.  
  224. void Class::printOn(ostream& strm) const
  225. {
  226.     strm << class_name;
  227. }
  228.  
  229. bool Class::isEqual(const Object& ob) const
  230. {
  231.     return this == (Class*)&ob;
  232. }
  233.  
  234. unsigned Class::size() const
  235. {
  236.     return inst_size;
  237. }
  238.  
  239. const Class* Class::isA() const
  240. {
  241.     return &class_Class;
  242. }
  243.  
  244. //====class.cpp====/\
  245.  
  246. ////////////////////////////////////////////////////////////
  247. // define class_Object
  248. ////////////////////////////////////////////////////////////
  249. const Class class_Object = Class (*(Class*)0, "Object", sizeof(Object));
  250.  
  251. ////////////////////////////////////////////////////////////
  252. // define class_Class
  253. ////////////////////////////////////////////////////////////
  254. const Class class_Class = Class( class_Object, "Class", sizeof(Class));
  255.  
  256. void DTerror(const char* s1, const char* s2)
  257. {
  258.     //cout << "DiskTutor Object Library error: "
  259.     cerr << "DiskTutor Object Library error: "
  260.          << s1
  261.          << " "
  262.          << s2
  263.          << "\n";
  264. }
  265.  
  266.